package com.aidex.web.controller.biz;

import java.util.List;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.domain.R;
import com.github.pagehelper.PageInfo;
import com.aidex.common.core.page.PageDomain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.poi.ExcelUtil;
import javax.validation.constraints.*;
import org.springframework.web.bind.annotation.*;
import com.aidex.common.core.controller.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import com.aidex.biz.domain.UserAddress;
import com.aidex.biz.service.UserAddressService;
import com.aidex.common.utils.SecurityUtils;

/**
 * 小程序用户地址Controller
 * @author sean
 * @email 2074844820@qq.com
 * @date 2022-09-22
 */
@RestController
@RequestMapping("/biz/bizUserAddress")
public class UserAddressController extends BaseController
{
    @Autowired
    private UserAddressService userAddressService;

    /**
     * 查询小程序用户地址列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizUserAddress:list')")
    @GetMapping("/list")
    public R<PageInfo> list(UserAddress userAddress, HttpServletRequest request, HttpServletResponse response)
    {
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            userAddress.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
         }
        userAddress.setPage(new PageDomain(request, response));
        return R.data(userAddressService.findPage(userAddress));
    }

    /**
     * 获取小程序用户地址详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizUserAddress:query')")
    @GetMapping(value = "/{id}")
    public R<UserAddress> detail(@PathVariable("id") String id)
    {
        return R.data(userAddressService.get(id));
    }

    /**
     * 新增小程序用户地址
     */
    @PreAuthorize("@ss.hasPermi('biz:bizUserAddress:add')")
    @Log(title = "小程序用户地址", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated  UserAddress userAddress)
    {
        return R.status(userAddressService.save(userAddress));
    }

    /**
     * 修改小程序用户地址
     */
    @PreAuthorize("@ss.hasPermi('biz:bizUserAddress:edit')")
    @Log(title = "小程序用户地址", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated UserAddress userAddress)
    {
        return R.status(userAddressService.save(userAddress));
    }


    /**
     * 删除小程序用户地址
     */
    @PreAuthorize("@ss.hasPermi('biz:bizUserAddress:remove')")
    @Log(title = "小程序用户地址", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids)
    {
        return R.status(userAddressService.deleteUserAddressByIds(ids));
    }

    /**
     * 获取最大编号
     */
    @PreAuthorize("@ss.hasPermi('biz:bizUserAddress:query')")
    @GetMapping("/findMaxSort")
    public R findMaxSort()
    {
        return R.data(userAddressService.findMaxSort(new UserAddress()));
    }

    /**
     * 导出小程序用户地址列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizUserAddress:export')")
    @Log(title = "小程序用户地址", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(UserAddress userAddress)
    {

            if(SecurityUtils.isAdmin(SecurityUtils.getUserId())){
                    userAddress.setCompanyId(null);
            }else{
                         userAddress.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
                     }
        List<UserAddress> list = userAddressService.findList(userAddress);
        ExcelUtil<UserAddress> util = new ExcelUtil<UserAddress>(UserAddress.class);
        return util.exportExcel(list, "小程序用户地址数据");
    }

}
